<?php
//ini_set('display_errors', 'On');
class Order extends BaseDB 
{
	function __construct()
	{
		$this->table = 'orders';
		parent::__construct();
	}
	
	/**
	 * 添加订单
	 *
	 * @param $data array('order_number', 'person_name', 'customer_id', 'order_machine_info_ids')
	 */
	public function add($data)
	{
//		$data['customer_id'] = (int)$data['customer_id'];
//		if (empty($data['order_number']) ||
//			empty($data['person_name']) ||
//			empty($data['customer_id']) ||
//			empty($data['order_machine_info_ids']))return false;
		
		$data['express_name'] = '';
		$data['express_tel'] = '';
		$data['status'] = 0;
		$data['last_modify_time'] = date("Y-m-d H:i:s");

		return $this->insert($data);
	}
	
	public function getInfoById($id)
	{
		if (empty($id))return false;
		return $this->select(array("id"=>$id));
	}
	
	public function getList($where, $start=0, $count=20)
	{
		if (!is_array($where) || empty($where))return false;
		return array(
			'total' => $this->getCount($where), 
			'data' => $this->select($where, $start, $count, "order by id desc")
		);
	}
	
	/**
	 * 已发货订单
	 * 给hp看的，包含（全部发货和部分发货订单）
	 * status 0：为发货，1：部分发货，2：全部发货
	 * @param unknown_type $start
	 * @param unknown_type $count
	 */
	public function getShippedList($start=0, $count=20) {
		
		//select count(id) from orders where status>0;
		$sql = "select count(id) from " . $this->table . " where status>?";
		$total = $this->db->getOne($sql, array(0));
		
		$start = max(0, (int)$start);
		$count = max(1, (int)$count);
		//select * from orders where status>0 order by id desc limit 0,20;
		$sql = "select * from " . $this->table . " where status>? order by id desc limit " . $start . "," .$count;
		return array(
			'total' => $total, 
			'data' => $this->db->getAll($sql, array(0))
		);
	}
	
	public function query($where, $start=0, $count=20)
	{
		if (!is_array($where))return false;
//		return $this->select($where, $start, $count);

		$condition = '1=1';
		$whereData = array();
		if (isset($where['sn'])) {
			$condition .= ' and sn=?';
			$whereData[] = $where['sn'];
		}
		if (isset($where['person_name'])) {
			$condition .= ' and person_name=?';
			$whereData[] = $where['person_name'];
		}
		if (isset($where['status'])) {
			$condition .= ' and status=?';
			$whereData[] = $where['status'];
		}
		if (isset($where['machine_sn'])) {
			$condition .= ' and machine_sn=?';
			$whereData[] = $where['machine_sn'];
		}
		if (isset($where['shippedTimeBegin'])) {
			$condition .= ' and shipped_time>?';
			$whereData[] = $where['shippedTimeBegin'];
			if (isset($where['shippedTimeEnd'])) {
				$condition .= ' and shipped_time<?';
				$whereData[] = $where['shippedTimeEnd']; 
			}
		}
		if (isset($where['orderTimeBegin'])) {
			$condition .= ' and create_time>?';
			$whereData[] = $where['orderTimeBegin'];
			if (isset($where['orderTimeEnd'])) {
				$condition .= ' and create_time<?';
				$whereData[] = $where['orderTimeEnd'];
			}
		}
		if (isset($where['customer_name'])) {
			$condition .= " and (customer_name like '%{$where['customer_name']}%')";
		}
		$totalCountSql = "select count(*) from $this->table where $condition";
		$totalCount = $this->db->getOne($totalCountSql, $whereData, false);
		
		$sql = "select * from $this->table where $condition order by id desc limit $start,$count";
		$data = $this->db->getAll($sql, $whereData, false);
		
		return array('total'=>$totalCount, 'data'=>$data);
	}
	
	public function genOrderSN()
	{
		$seed = 'RS';
		return $seed . date('ymdH') . strtoupper(substr(md5($seed . time(). rand(10001, 99999)), 6, 8));
	}
	
	public function signShippedById($id, $expressName, $expressTel, $expressID, $shippedTime, $remark, $shippedStatus, $shippedRemark)
	{
		if (empty($id) || empty($expressID) || empty($expressName))return false;
		if (empty($shippedTime)) {
	    	$shippedTime = date('Y-m-d H:i:s');
	    }
		return $this->update(array('status'=>(1+(int)$shippedStatus), 'shipped_time'=>$shippedTime, 'express_name'=>$expressName, 'express_tel'=>$expressTel, 'express_id'=>$expressID, 'remark'=>$remark,'last_modify_time'=>date('Y-m-d H:i:s'), 'shipped_remark'=>$shippedRemark), array('id'=>$id));
	}
	
	public function signShippedForUnFinishedById($id, $shippedStatus, $shippedRemark, $remark)
	{
		if (empty($id))return false;
		return $this->update(array('status'=>(1+(int)$shippedStatus), 'shipped_remark'=>$shippedRemark, 'remark'=>$remark), array('id'=>$id));
	}
	
	public function modify($data, $where) {
		if (empty($data) || empty($where)) {
			return false;
		}
		
		return $this->update($data, $where);
	}
}
?>